package com.sense.kukoo.common.utils

import java.lang.Exception
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter

class DataTimeUtil {
    companion object{
        private val TAG: String = DataTimeUtil::class.java.simpleName

        private val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")
        private val formatter2 = DateTimeFormatter.ofPattern("MM/dd HH:mm")
        private val offset = ZoneId.systemDefault().rules.getOffset(Instant.now())

        fun timeStamp2Date(time: Long): String{
            //val ldt = LocalDateTime.ofEpochSecond(time/1000, (time % 1000).toInt(), offset)
            val ldt = LocalDateTime.ofEpochSecond(time/1000, 0, offset)
            return ldt.format(formatter)
        }

        fun timeStamp2Date2(time: Long): String{
            //val ldt = LocalDateTime.ofEpochSecond(time/1000, (time % 1000).toInt(), offset)
            val ldt = LocalDateTime.ofEpochSecond(time/1000, 0, offset)
            return ldt.format(formatter2)
        }

        //"2022-08-11T18:40:32" -> 1660214432853
        fun date2TimeStamp(date: String): Long{
            var time = date
            if(date.length > 19){
                time = date.subSequence(0, 19).toString()
            }
            val ldt = LocalDateTime.parse(time, formatter)
            return ldt.toInstant(offset).toEpochMilli()
        }

        //"2022-08-11T18:40:32" -> just_now
        fun dateTransform1(date: String): String{
            val timeStamp: Long
            try {
                timeStamp = date2TimeStamp(date)
            } catch (e: Exception){
                return ""
            }
            val time = System.currentTimeMillis() - timeStamp
            val sec :Long = time / 1000
            val min = sec / 60
            val hour = min / 60
            val day = hour / 24
            return if(sec < 60){
                ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.subscriptions_updated_just_now)
            }else if(min < 60){
                ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.subscriptions_updated_min, min)
            }else if(hour < 24){
                ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.subscriptions_updated_hour, hour)
            }else if(day < 7){
                ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.subscriptions_updated_day, day)
            }else if(day < 31){
                val week = day / 7
                ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.subscriptions_updated_week, week)
            }else if(day < 365){
                val month = day / 30
                ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.subscriptions_updated_month, month)
            }else {
                val year = day / 365
                ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.subscriptions_updated_year, year)
            }
        }

        //"2022-08-11T18:40:32" -> 07/05 22:04
        fun dateTransform2(date: String): String{
            val timeStamp: Long
            try {
                timeStamp = date2TimeStamp(date)
            } catch (e: Exception){
                return ""
            }
            return timeStamp2Date2(timeStamp)
        }

        //time: 秒
        fun calculateTime(time: Int): String {
            return if (time >= 60) {
                val minute = time / 60
                val second = time % 60
                (if (minute < 10) "0$minute" else "" + minute) + if (second < 10) ":0$second" else ":$second"
            } else {
                if (time < 10) "00:0$time" else "00:$time"
            }
        }

    }
}